#include "polygon.h"
#include "point2.h"
#include "rect.h"
#include "tools.h"

/*
void dotProduct(Point2* p1, Point2* p2){

	ret =p1->x*p2->x +p1->y*p2->y
}
*/
void addVectToRect(Rect* r, Point2* pt){

	r->yHi +=pt->y;
	r->yLo +=pt->y;
	r->xHi +=pt->x;
	r->xLo +=pt->x;
}

void addVectToPolygon(Polygon2* p, Point2* pt){
//
//	int i;
//
//	for(i=0; i<p->nElem; i++){
//		p->pList[i] =p->pList[i] + *pt;
//	}
}

Rect* createBoundingBox(Polygon2* poly){
	
	int i;
	Rect* box;

	box =new Rect(poly->pList[0]->x, poly->pList[0]->y, poly->pList[0]->x ,poly->pList[0]->y);
	// find polygon rect
	for(i=1; i<poly->maxElem; i++){
	
		if(poly->pList[i]->x > box->xHi)
			box->xHi =poly->pList[i]->x;
		if(poly->pList[i]->x < box->xLo)
			box->xLo =poly->pList[i]->x;
		if(poly->pList[i]->y > box->yHi)
			box->yHi =poly->pList[i]->y;
		if(poly->pList[i]->y < box->yLo)
			box->yLo =poly->pList[i]->y;
	}

	return box;
}


/*
void LoadTexture(char *filename, long texture_id, long mag_filter,
	long min_filter, long wrap_type)
{
	TEXTURE texture;
	if (!texture.Load (filename)) 
		return;
	glBindTexture (GL_TEXTURE_2D, TextureName[texture_id]);
	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, texture.info_header.biWidth,
	texture.info_header.biHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, texture.data);
	gluBuild2DMipmaps (GL_TEXTURE_2D, GL_RGB, texture.info_header.biWidth, texture.info_header.biHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, texture.data);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_type);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_type);
	glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	texture.Release();
}

void LoadTransparentTexture(char *filename, long texture_id, long mag_filter,
	long min_filter, long wrap_type, unsigned char trans_r, unsigned char
	trans_g, unsigned char trans_b)
{
	TEXTURE texture;
	unsigned char *alpha_texture=NULL;
	long texel;
	if (!texture.Load (filename)) 
		return;
	alpha_texture = new unsigned char[texture.info_header.biWidth*texture.info_header.biHeight*4+1];
	for (texel = 0; texel < texture.info_header.biWidth*texture.info_header.biHeight; texel++){

		alpha_texture[texel*4] = texture.data[texel*3];
		alpha_texture[texel*4+1]= texture.data[texel*3+1];
		alpha_texture[texel*4+2]= texture.data[texel*3+2];
		if (texture.data[texel*3] == trans_b &&	texture.data[texel*3+1]== trans_g && texture.data[texel*3+2]== trans_r) 
			alpha_texture[texel*4+3] = 0;
		else 
			alpha_texture[texel*4+3] = 255;
	}
	glBindTexture(GL_TEXTURE_2D, TextureName[texture_id]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.info_header.biWidth, texture.info_header.biHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, alpha_texture);
	gluBuild2DMipmaps (GL_TEXTURE_2D, GL_RGBA, texture.info_header.biWidth,	texture.info_header.biHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, alpha_texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_type);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_type);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	delete[] alpha_texture;
	alpha_texture = NULL;
	texture.Release();
}
*/
